#include <bits/stdc++.h>

using namespace std;

const int maxn=10;
int a[maxn],b[maxn],fac[maxn];

inline int read(){
    char c;int r;bool f=0;
    for(c=getchar();c>'9'||c<'0';c=getchar()) if(c=='-') f=1;
    for(r=0;c<='9'&&c>='0';c=getchar()) r=r*10+c-'0';
    if(f) return -r;
    else return r;
}

inline int Cantor(const int *arr,const int &n){
    int ans=0;
    for(int i=0;i<n;i++){
        int s=0;
        for(int j=i+1;j<n;j++) if(arr[j]<arr[i]) s++;
        ans+=s*fac[n-i-1];
    }
    return ans;
}

inline void InvCant(int *dest,int k,const int &n){
    bool *vis=new bool[n+1];
    memset(vis,0,sizeof(bool)*(n+1));
    for(int i=0;i<n;i++){
        int j,t;
        t=k/fac[n-i-1];
        k%=fac[n-i-1];
        for(j=1;j<=n;j++){
            if(!vis[j]){
                if(t==0) break;
                t--;
            }
        }
        dest[i]=j;
        vis[j]=1;
    }
    delete[] vis;
}

int main(){
    int n;

    n=read();
    fac[0]=1;
    for(int i=1;i<=n;i++) fac[i]=i*fac[i-1];
    for(int i=0;i<n;i++) a[i]=i+1;
    for(int i=0;i<fac[n];i++){
        for(int j=0;j<n;j++) printf("%d ",a[j]);
        int c;
        c=Cantor(a,n);
        printf("%d ",c);
        InvCant(b,c,n);
        for(int j=0;j<n;j++) printf("%d ",b[j]);
        putchar('\n');
        next_permutation(a,a+n);
    }

    return 0;
}
